import axios from "axios";
import { Toast } from "antd-mobile/2x"
const request = axios.create({
    timeout: 20000,
    baseURL: "/api"
})



let num = 0;
request.interceptors.request.use(config => {
    num++;
    Toast.show({
        content: '加载中...',
        icon: "loading",
        duration: 0,
        maskClickable: false,
    })
    const token = localStorage.getItem("token");
    if (token) {
        config.headers.authorization = token;
    }
    return config;
}, (err) => {
    return Promise.reject(err);
})

request.interceptors.response.use(response => {
    let res = response.data;
    num--;
    if (num === 0) {
        Toast.clear()
    }

    switch (res.status) {
        case 400: {
            window.location.href = "/login"
            return res;
        }
        case 500: {
            window.location.href = "/login"
            return res;
        }
        default:
            return res;
    }
}, (err) => {
    let res = err.response;
    console.log(err);
    switch (res.status) {
        case 500: {
            console.log(1);
            window.location.href = "/login"
            return res;
        }
        default:
            return Promise.reject(err);
    }
})
export default request;